package com.example.online_chat.api;

import com.example.online_chat.model.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
public class FriendReqAPI {
    @Autowired
    private FriendReqMapper friendReqMapper;

    @Autowired
    private FriendMapper friendMapper;

    @GetMapping("/searchUser")
    public Object searchUser(String searchElement, @SessionAttribute("user") User user) {
        List<FriendReq> resp = friendReqMapper.getSearchList(searchElement, user.getUsername());
        Set<Integer> friendSet = new HashSet<>();
        Set<Integer> initiatorSet = new HashSet<>();
        Set<Integer> receiverSet = new HashSet<>();

        List<Friend> friends = friendMapper.getFriendList(user.getUserId());
        for (Friend friend : friends) {
            friendSet.add(friend.getFriendId());
        }

        List<FriendReq> initiators = friendReqMapper.getInitiator(user.getUserId());
        for (FriendReq initiator : initiators) {
            initiatorSet.add(initiator.getUserId());
        }

        List<FriendReq> receivers = friendReqMapper.getReceiver(user.getUserId());
        for (FriendReq receiver : receivers) {
            receiverSet.add(receiver.getUserId());
        }

        for (FriendReq friendReq : resp) {
            if (friendSet.contains(friendReq.getUserId())) {
                friendReq.setState(3);
            } else if (initiatorSet.contains(friendReq.getUserId())) {
                friendReq.setState(0);
            } else if (receiverSet.contains(friendReq.getUserId())) {
                friendReq.setState(1);
            } else {
                friendReq.setState(2);
            }
        }

        return resp;
    }

    @PostMapping("/sendFriendReq")
    public synchronized void sendFriendReq(String message, String receiverName, @SessionAttribute("user") User user) {
        int receiver = friendReqMapper.getUserIdByName(receiverName);

        List<Integer> reqList = friendReqMapper.searchFriend(user.getUserId());
        for (Integer e : reqList) {
            if (e == receiver) {
                return;
            }
        }

        FriendReq friendReq = new FriendReq();

        friendReq.setInitiator(user.getUserId());
        friendReq.setReceiver(receiver);
        friendReq.setMessage(message);

        friendReqMapper.addFriendReq(friendReq);
    }

    @GetMapping("/getFriendReq")
    public List<FriendReq> getFriendReq(@SessionAttribute("user") User user) {
        return friendReqMapper.getInitiator(user.getUserId());
    }

    @PostMapping("/rejectFriendReq")
    private void rejectFriendReq(Integer requestId, @SessionAttribute("user") User user) {
        friendReqMapper.delete(requestId, user.getUserId());
    }

    @PostMapping("/acceptFriendReq")
    private void acceptFriendReq(Integer requestId, @SessionAttribute("user") User user) {
        // 首先先删除好友请求表的内容
        friendReqMapper.delete(requestId, user.getUserId());

        // 添加好友
        friendReqMapper.addFriend(user.getUserId(), requestId);
    }
}
